package vip.xiaonuo.supplier.modular.setting.reports.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.supplier.modular.setting.reports.entity.PublicReports;
import vip.xiaonuo.supplier.modular.setting.reports.mapper.PublicReportsMapper;
import vip.xiaonuo.supplier.modular.setting.reports.param.PublicReportsAddParam;
import vip.xiaonuo.supplier.modular.setting.reports.param.PublicReportsEditParam;
import vip.xiaonuo.supplier.modular.setting.reports.param.PublicReportsIdParam;
import vip.xiaonuo.supplier.modular.setting.reports.param.PublicReportsPageParam;
import vip.xiaonuo.supplier.modular.setting.reports.service.PublicReportsService;

import java.util.List;

/**
 * 打印格式Service接口实现类
 *
 * @author Mr.wu
 * @date  2025/04/01 15:08
 **/
@Service
public class PublicReportsServiceImpl extends ServiceImpl<PublicReportsMapper, PublicReports> implements PublicReportsService {

    @Override
    public Page<PublicReports> page(PublicReportsPageParam publicReportsPageParam) {
        QueryWrapper<PublicReports> queryWrapper = new QueryWrapper<PublicReports>().checkSqlInjection();
        //数据归属
        if(ObjectUtil.isNotEmpty(publicReportsPageParam.getReportType())){
            queryWrapper.lambda().eq(PublicReports::getReportType, publicReportsPageParam.getReportType());
        }
        queryWrapper.lambda().eq(PublicReports::getCompanyId, StpLoginUserUtil.getLoginUser().getCompanyId());
        if(ObjectUtil.isAllNotEmpty(publicReportsPageParam.getSortField(), publicReportsPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(publicReportsPageParam.getSortOrder());
            queryWrapper.orderBy(true, publicReportsPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(publicReportsPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(PublicReports::getId);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(PublicReportsAddParam publicReportsAddParam) {
        PublicReports publicReports = BeanUtil.toBean(publicReportsAddParam, PublicReports.class);
        //数据归属
        publicReports.setCompanyId(StpLoginUserUtil.getLoginUser().getCompanyId());
        this.save(publicReports);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(PublicReportsEditParam publicReportsEditParam) {
        PublicReports publicReports = this.queryEntity(publicReportsEditParam.getId());
        BeanUtil.copyProperties(publicReportsEditParam, publicReports);
        this.updateById(publicReports);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<PublicReportsIdParam> publicReportsIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(publicReportsIdParamList, PublicReportsIdParam::getId));
    }

    @Override
    public PublicReports detail(PublicReportsIdParam publicReportsIdParam) {
        return this.queryEntity(publicReportsIdParam.getId());
    }

    @Override
    public PublicReports queryEntity(String id) {
        PublicReports publicReports = this.getById(id);
        if(ObjectUtil.isEmpty(publicReports)) {
            throw new CommonException("打印格式不存在，id值为：{}", id);
        }
        return publicReports;
    }
}
